cap log close
clear all
set maxvar 32000
set matsize 10000
set more off

cd "C:\Users\ritag\Dropbox\Mexico Labor\"

clear all
cap log close
set more off

local origdata 	"D:\Mexico\Data_source\ENOE\Data\"
local savedata 	"C:\Users\rgi021\"
local rollout 	"C:\Users\rgi021\"
*---------------------------------------------------------------------------------------------------
* Base
use  "`savedata'sample_final", clear
	
*---------------------------------------------------------------------------------------------------
**# Ajustes nas bases
*---------------------------------------------------------------------------------------------------
tab edu, ge(deduc_)
keep if time == 1
merge m:1 st mun using "$datamun/mun_rollout_data_simple.dta" , nogen keep(3)
drop ent  mun2 municip municip2 mun_code

ge age1 = 1 if eda <30
replace age1 = 2 if eda >= 30 & eda < 40
replace age1 = 3 if eda >= 40 & eda < 50
replace age1 = 4 if eda >= 50 & eda < 65
tab age1, ge(dage1_)

ge age = eda
ge age2 = age*age

* average educ by sector-municip
ge aux = educationhigh if period == 1
bys mun situation_initial: egen mean_educ = max(aux)
drop aux 

ge dsp_dchild0_14 = dchild0_14*dsp
	
	
ge lw = ln(wfinal) 
ge s_lw = ln(s_wfinal) 

tab situation1, ge(dsituation_)
tab s_situation1, ge(ds_situation_)

drop if newq== yq(2003,3) | newq== yq(2003,4)

*---------------------------------------------------------------------------------------------------
**# Table 1
*---------------------------------------------------------------------------------------------------
	
** before/ after SP 
ge 		timenew = 0 if tsince <= -1 
replace timenew = 1 if tsince >= 1
	
ge informal = 1 if situation1 == 3
replace informal = 0 if situation1 == 2

ge s_informal = 1 if s_situation1 == 3
replace s_informal = 0 if s_situation1 == 2
	
for num 0 1: ge lwX = lw if informal == X
for num 0 1: ge s_lwX = s_lw if s_informal == X
	
for num 0 1: cap drop wfinalX s_wfinalX
for num 0 1: ge wfinalX = wfinal if informal == X
for num 0 1: ge s_wfinalX = s_wfinal if s_informal == X	
	

* stocks and transitions - with kids
*before
	su informalh dchild0_14 mff mfi mfn mif mnf mii min_ mni mnn lw1 lw0 s_lw1 s_lw0 if educationhigh == 0 & timenew == 0 & dchild0_14 ==1, separator(0) 
	su informalh dchild0_14 mff mfi mfn mif mnf mii min_ mni mnn lw1 lw0 s_lw1 s_lw0 if educationhigh == 1 & timenew == 0 & dchild0_14 ==1, separator(0) 
*after
	su informalh dchild0_14 mff mfi mfn mif mnf mii min_ mni mnn lw1 lw0 s_lw1 s_lw0 if educationhigh == 0 & timenew == 1 & dchild0_14 ==1, separator(0) 
	su informalh dchild0_14 mff mfi mfn mif mnf mii min_ mni mnn lw1 lw0 s_lw1 s_lw0 if educationhigh == 1 & timenew == 1 & dchild0_14 ==1, separator(0) 

* stocks and transitions - without kids
*before
	su informalh dchild0_14 mff mfi mfn mif mnf mii min_ mni mnn lw1 lw0 s_lw1 s_lw0 if educationhigh == 0 & timenew == 0 & dchild0_14 ==0, separator(0) 
	su informalh dchild0_14 mff mfi mfn mif mnf mii min_ mni mnn lw1 lw0 s_lw1 s_lw0 if educationhigh == 1 & timenew == 0 & dchild0_14 ==0, separator(0) 
*after
	su informalh dchild0_14 mff mfi mfn mif mnf mii min_ mni mnn lw1 lw0 s_lw1 s_lw0 if educationhigh == 0 & timenew == 1 & dchild0_14 ==0, separator(0) 
	su informalh dchild0_14 mff mfi mfn mif mnf mii min_ mni mnn lw1 lw0 s_lw1 s_lw0 if educationhigh == 1 & timenew == 1 & dchild0_14 ==0, separator(0) 

	
log close	
	
*---------------------------------------------------------------------------------------------------
**# TABLES - Regressões
*---------------------------------------------------------------------------------------------------
	ge allw07  = 1 if year <= 2007
	ge allw07_1 = 1 if year <= 2007 & dchild0_14 ==1
	ge allw07_0 = 1 if year <= 2007 & dchild0_14 ==0
	ge allw12 = 1 if year > 2007
	
	local supply "nhospps_h_b nhospps_hc_b totmedps_h_b"
	local loc_controls "large_city im_00 lnpop00_mun shpob0_4 shpocusecp00_mun shpocusecs00_mun shpocusect00_mun"
	local fam_controls "dage1_* male deduc_*"

	collapse (mean) wfinal0 wfinal1 s_wfinal0 s_wfinal1 pop00_mun informalh ///
		mff mfi mfn mif mnf mii min_ mni mnn  ///
		dnf_1 dni_1 dfn_1 dfi_1 din_1 dif_1 dni_df_1 dni_di_1 dnf_2 dni_2 dfn_2 dfi_2 din_2 dif_2 dni_df_2 dni_di_2 ///
		`fam_controls' `loc_controls' `supply'  tsince  year  allw07 dsp pob00, by(st mun newq educationhigh )

	egen newmun = group(st mun)

	tab st, ge(st_)
	egen groupy = group(year)
	for num 1/32: ge stX_y =  st_X*groupy 
	for num 1/32: ge stX_y2  = stX_y^2


* Other trends & year fe interactions: baseline characteristics (by type)
foreach var in `loc_controls' `supply'  {
     ge `var'_y=`var'*groupy
}

		local fam_controls "dage1_* male deduc_*"
	local supply "nhospps_h_b_y nhospps_hc_b_y totmedps_h_b_y"
	local loc_controls "large_city_y im_00_y lnpop00_mun_y"

	label var lw0 		"Ln(w) Formal" 
	label var lw1 		"Ln(w) Informal" 
	label var s_lw0 	"Ln(w) Formal"
	label var s_lw1 	"Ln(w) Informal" 
	label var informalh "Informal"
	label var mff 		"\m_{ff}"
	label var mfi 		"\m_{fi}"
	label var mfn 		"\m_{fn}"
	label var mif 		"\m_{if}"
	label var mnf 		"\m_{nf}"
	label var mii 		"\m_{ii}"
	label var min_ 		"\m_{in}"
	label var mni 		"\m_{ni}"
	label var mnn		"\m_{nn}"
	label var dsp 		"SP" 

* Regressões	

foreach var in lw0 lw1 s_lw0 s_lw1 informalh mff mfi mfn mif mnf mii min_ mni mnn   {
	
		foreach j in 0 1 {	
		foreach n in 0 1 {
		
			preserve
			keep if dchild0_14==`n' &  educationhigh == `j'
		
			xtset newmun newq

			eststo `var'`n'`j': xi: qui xtreg `var' dsp i.newq `fam_controls' `loc_controls' `supply', fe cluster(newmun)
			su `var' if year== 2001 & e(sample), d
			estadd r(mean): `var'`n'`j'
		
			xtset, clear
			restore
		}
	}
}

* table 2
	local j "0"
	local n "1"
	esttab informalh`n'`j' lw0`n'`j' lw1`n'`j' s_lw0`n'`j' s_lw1`n'`j' using "base.tex", replace booktabs legend ///
		keep(dsp) cells(`"b(star fmt(3))"' `"se(fmt(3) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("\midrule" "\multicolumn{5}{l}{Panel A: Low Education - With Children} \\") postfoot("\vspace{0.05cm} \\") ///
		fragment stats(mean emp N, fmt(%9.3g)) label mgroup(none) collabels(none) eqlabels(none) 
		
	local j "0"
	local n "0"	
	esttab informalh`n'`j' lw0`n'`j' lw1`n'`j' s_lw0`n'`j' s_lw1`n'`j'  using "base.tex", append booktabs legend ///
		keep(dsp) cells(`"b(star fmt(3))"' `"se(fmt(3) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines prehead("\multicolumn{5}{l}{Panel B: Low Education - Without Children} \\") postfoot("\vspace{0.05cm} \\") ///
		fragment stats(mean emp N, fmt(%9.3g)) label mgroup(none) nonumbers mlabel(none) collabels(none) eqlabels(none) 


	local j "1"
	local n "1"	
	esttab informalh`n'`j' lw0`n'`j' lw1`n'`j' s_lw0`n'`j' s_lw1`n'`j' using "base.tex", append booktabs legend ///
		keep(dsp) cells(`"b(star fmt(3))"' `"se(fmt(3) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines prehead("\multicolumn{5}{l}{Panel C: High Education - With Children} \\") postfoot("\vspace{0.05cm} \\") ///
		fragment stats(mean emp N, fmt(%9.3g)) label mgroup(none) nonumbers mlabel(none) collabels(none) eqlabels(none) 

	local j "1"
	local n "0"	
	esttab informalh`n'`j' lw0`n'`j' lw1`n'`j' s_lw0`n'`j' s_lw1`n'`j' using "base.tex", append booktabs legend ///
		keep(dsp) cells(`"b(star fmt(3))"' `"se(fmt(3) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines prehead("\multicolumn{5}{l}{Panel D: High Education - Without Children} \\") postfoot("\vspace{0.05cm} \\") ///
		fragment stats(mean emp N, fmt(%9.3g)) label mgroup(none) nonumbers mlabel(none) collabels(none) eqlabels(none) 
		

		
* robustness - Table A3
foreach var in informalh lw1 {
	
		foreach j in 0 1 {	
		foreach n in 0 1 {
		
			preserve
			keep if dchild0_14==`n' &  educationhigh == `j'
		
			xtset newmun newq

			eststo `var'`n'`j'1: xi: qui xtreg `var' dsp i.newq st*_y st*_y2 `fam_controls' `loc_controls' `supply', fe cluster(newmun)

			eststo `var'`n'`j'2: xi: qui xtreg `var' dsp i.newq `fam_controls', fe cluster(newmun)
			
			xtset, clear
			restore
		}
	}
}

local j "0"
local n "1"
esttab informalh`n'`j'1 informalh`n'`j'2 using "robustness.tex", replace booktabs legend ///
	keep(dsp) cells(`"b(star fmt(3))"' `"se(fmt(3) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("\midrule" "\multicolumn{5}{l}{Panel A: Low Education - With Children} \\") postfoot("\vspace{0.05cm} \\") ///
	fragment stats(mean emp N, fmt(%9.3g)) label mgroup(none) collabels(none) eqlabels(none) 
local j "1"
local n "0"
esttab lw1`n'`j'1 lw1`n'`j'2 using "robustness.tex", append booktabs legend ///
	keep(dsp) cells(`"b(star fmt(3))"' `"se(fmt(3) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) noobs nolines posthead("\midrule" "\multicolumn{5}{l}{Panel B: High Education - Without Children} \\") postfoot("\vspace{0.05cm} \\") ///
	fragment stats(mean emp N, fmt(%9.3g)) label mgroup(none) collabels(none) eqlabels(none) 

		
		
*---------------------------------------------------------------------------------------------------
**# TABLE - Regressões - com dummy de health shock 
*---------------------------------------------------------------------------------------------------
* Base
	use  "`savedata'/sample_final", clear
    tab any_hs dchild0_14, mi
	
* Ajustes na base	

	tab edu, ge(deduc_)
	keep if time == 1
	merge m:1 st mun using "$datamun/mun_rollout_data_simple.dta" , nogen keep(3)
	drop ent  mun2 municip municip2 mun_code

	tab t_loc, ge(dt_loc_)
	ge age1 = 1 if eda <30
	replace age1 = 2 if eda >= 30 & eda < 40
	replace age1 = 3 if eda >= 40 & eda < 50
	replace age1 = 4 if eda >= 50 & eda < 65
	tab age1, ge(dage1_)

	ge age = eda
	ge age2 = age*age

	* average educ by sector-municip
	ge aux = educationhigh if period == 1
	bys mun situation_initial: egen mean_educ = max(aux)
	drop aux 

	ge dsp_dchild0_14 = dchild0_14*dsp
	
	ge lw = ln(wfinal) 
	ge s_lw = ln(s_wfinal) 

	tab situation1, ge(dsituation_)
	tab s_situation1, ge(ds_situation_)

	drop if newq== yq(2003,3) | newq== yq(2003,4)

* Variáveis para regressão 

	ge allw07  = 1 if year <= 2007
	ge allw07_1 = 1 if year <= 2007 & dchild0_14 ==1
	ge allw07_0 = 1 if year <= 2007 & dchild0_14 ==0
	ge allw12 = 1 if year > 2007

	ge informal = 1 if situation1 == 3
	replace informal = 0 if situation1 == 2
	count if lw ==. & informal !=.
	count if  lw ==. & informal !=. & hrsocup !=.
	su wfinal hrsocup if  lw ==. & informal !=. & hrsocup < 8

	ge s_informal = 1 if s_situation1 == 3
	replace s_informal = 0 if s_situation1 == 2

	for num 0 1: ge lwX = lw if informal == X
	for num 0 1: ge s_lwX = s_lw if s_informal == X
	
	local supply "nhospps_h_b nhospps_hc_b totmedps_h_b"
	local loc_controls "large_city im_00 lnpop00_mun shpob0_4 shpocusecp00_mun shpocusecs00_mun shpocusect00_mun"
	local fam_controls "dage1_* male deduc_*"

	collapse (mean) lw0 lw1 s_lw1 s_lw0 pop00_mun informalh ///
		mff mfi mfn mif mnf mii min_ mni mnn  ///
		`fam_controls' `loc_controls' `supply'  tsince  year  ///
		allw07 dsp pob00 , by(st mun newq educationhigh dchild0_14 any_hs)

	egen newmun = group(st mun) // -> unidade "i" no painel

	tab st, ge(st_)
	egen groupy = group(year)
	for num 1/32: ge stX_y =  st_X*groupy 
	for num 1/32: ge stX_y2  = stX_y^2

* Other trends & year fe interactions: baseline characteristics (by type)
foreach var in `loc_controls' `supply'  {
     ge `var'_y=`var'*groupy
}

	local fam_controls "dage1_* male deduc_*"
	local supply "nhospps_h_b_y nhospps_hc_b_y totmedps_h_b_y"
	local loc_controls "large_city_y im_00_y lnpop00_mun_y"

	label var lw0 		"Ln(w) Formal" 
	label var lw1 		"Ln(w) Informal" 
	label var s_lw0 	"Ln(w) Formal"
	label var s_lw1 	"Ln(w) Informal" 
	label var informalh "Informal"
	label var mff 		"\m_{ff}"
	label var mfi 		"\m_{fi}"
	label var mfn 		"\m_{fn}"
	label var mif 		"\m_{if}"
	label var mnf 		"\m_{nf}"
	label var mii 		"\m_{ii}"
	label var min_ 		"\m_{in}"
	label var mni 		"\m_{ni}"
	label var mnn		"\m_{nn}"
	label var dsp 		"SP" 
	
	gen d_anyhs2 = any_hs*dsp
	lab var d_anyhs2 "SP * Health shock"
	lab var any_hs "Health shock"
	tab any_hs year, mi
	
	
foreach var in  informalh  {
	
		foreach j in 0 1 {	
		foreach n in 0 1 {
		
			preserve
			keep if dchild0_14==`n' &  educationhigh == `j'
		
			xtset newmun 

			eststo `var'`n'`j': xi: qui xtreg `var' dsp any_hs d_anyhs2 i.newq `fam_controls' `loc_controls' `supply', fe cluster(newmun)
            test dsp + d_anyhs2 = 0
			scalar pval = r(p)
            estadd scalar pval
			
			qui su `var' if year== 2001 & e(sample), d
			estadd r(mean): `var'`n'`j'
			su `var' if year== 2001 & e(sample) & any_hs==0 , meanonly
			estadd scalar mean0 = r(mean)  
			su `var' if year== 2001 & e(sample) & any_hs==1 , meanonly
			estadd scalar mean1 = r(mean)  

			xtset, clear
			restore
		}
	}
}


* Table A3
	local j "0"
	local n "1"
	esttab informalh`n'`j' lw0`n'`j' lw1`n'`j' s_lw0`n'`j' s_lw1`n'`j' using "base_d_anyhs.tex", replace booktabs legend ///
		keep(dsp any_hs d_anyhs2) cells(`"b(star fmt(3))"' `"se(fmt(3) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) ///
		noobs nolines posthead("\midrule" "\multicolumn{5}{l}{Panel A: Low Education - With Children} \\") postfoot("\vspace{0.05cm} \\") ///
		fragment stats(mean mean0 mean1 pval emp N, fmt(%9.3g)) label mgroup(none) collabels(none) eqlabels(none) 
		
	local j "0"
	local n "0"	
	esttab informalh`n'`j' lw0`n'`j' lw1`n'`j' s_lw0`n'`j' s_lw1`n'`j'  using "base_d_anyhs.tex", append booktabs legend ///
		keep(dsp any_hs d_anyhs2) cells(`"b(star fmt(3))"' `"se(fmt(3) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) ///
		noobs nolines prehead("\multicolumn{5}{l}{Panel B: Low Education - Without Children} \\") postfoot("\vspace{0.05cm} \\") ///
		fragment stats(mean mean0 mean1 pval emp N, fmt(%9.3g)) label mgroup(none) nonumbers mlabel(none) collabels(none) eqlabels(none) 


	local j "1"
	local n "1"	
	esttab informalh`n'`j' lw0`n'`j' lw1`n'`j' s_lw0`n'`j' s_lw1`n'`j' using "base_d_anyhs.tex", append booktabs legend ///
		keep(dsp any_hs d_anyhs2) cells(`"b(star fmt(3))"' `"se(fmt(3) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) ///
		noobs nolines prehead("\multicolumn{5}{l}{Panel C: High Education - With Children} \\") postfoot("\vspace{0.05cm} \\") ///
		fragment stats(mean mean0 mean1 pval emp N, fmt(%9.3g)) label mgroup(none) nonumbers mlabel(none) collabels(none) eqlabels(none)  

	local j "1"
	local n "0"	
	esttab informalh`n'`j' lw0`n'`j' lw1`n'`j' s_lw0`n'`j' s_lw1`n'`j' using "base_d_anyhs.tex", append booktabs legend ///
		keep(dsp any_hs d_anyhs2) cells(`"b(star fmt(3))"' `"se(fmt(3) par)"') starlevels(* 0.1 ** 0.05 *** 0.01) ///
		noobs nolines prehead("\multicolumn{5}{l}{Panel D: High Education - Without Children} \\") postfoot("\vspace{0.05cm} \\") ///
		fragment stats(mean mean0 mean1 pval emp N, fmt(%9.3g)) label mgroup(none) nonumbers mlabel(none) collabels(none) eqlabels(none)  
		

clear
		
